CREATE TABLE [dbo].[TCMOVI_CAJA]
(
[CO_EMPR] [dbo].[TD_VC_002] NOT NULL,
[CO_CAJA] [dbo].[TD_VC_008] NOT NULL,
[AA_CAJA] [dbo].[TD_VC_004] NOT NULL,
[MM_CAJA] [dbo].[TD_VC_002] NOT NULL,
[TI_MOVI] [dbo].[TD_VC_001] NOT NULL,
[NU_COMP_CAJA] [dbo].[TD_VC_008] NOT NULL,
[FE_OPER] [dbo].[TD_DT_001] NOT NULL,
[CO_TIPO_OPER] [dbo].[TD_VC_003] NOT NULL,
[TI_ENTI] [dbo].[TD_VC_001] NULL,
[CO_ENTI] [dbo].[TD_VC_020] NULL,
[FA_TIPO_CAMB] [dbo].[TD_NU_015_006] NOT NULL CONSTRAINT [DF__TCMOVI_CA__FA_TI__2057CCD0] DEFAULT ((1.000)),
[CO_UNID_CONC] [dbo].[TD_VC_003] NOT NULL,
[DE_ENTI] [dbo].[TD_VC_100] NULL,
[IM_MOVI] [dbo].[TD_NU_016_004] NOT NULL CONSTRAINT [DF__TCMOVI_CA__IM_MO__214BF109] DEFAULT ((0)),
[CO_ENTR_REND] [dbo].[TD_VC_003] NULL,
[ST_CONT] [dbo].[TD_VC_001_INTE] NOT NULL CONSTRAINT [DF__TCMOVI_CA__ST_CO__22401542] DEFAULT ('N'),
[CO_BANC_ENRE] [dbo].[TD_VC_003] NULL,
[NU_PROC] [dbo].[TD_IN_001] NULL CONSTRAINT [DF__TCMOVI_CA__NU_PR__24285DB4] DEFAULT ((0)),
[DE_OBSE] [dbo].[TD_VC_100] NULL,
[NU_CNTA_ENRE] [dbo].[TD_VC_020] NULL,
[CO_TIPO_DOCU] [dbo].[TD_VC_003] NULL,
[CO_CAJA_ENRE] [dbo].[TD_VC_008] NULL,
[NU_DOCU] [dbo].[TD_VC_020] NULL,
[CO_BANC_CHEQ] [dbo].[TD_VC_003] NULL,
[FE_DISP] [dbo].[TD_DT_001] NULL,
[AA_ENRE] [dbo].[TD_VC_004] NULL,
[CO_CAJA_DEST] [dbo].[TD_VC_008] NULL,
[MM_ENRE] [dbo].[TD_VC_002] NULL,
[CO_BANC_DEST] [dbo].[TD_VC_003] NULL,
[NU_CNTA_DEST] [dbo].[TD_VC_020] NULL,
[NU_SOLI] [dbo].[TD_VC_010] NULL,
[NU_COMP_ENRE] [dbo].[TD_VC_008] NULL,
[CO_MONE_DEST] [dbo].[TD_VC_003] NULL,
[AA_DEST] [dbo].[TD_VC_004] NULL,
[NU_REND_GAST] [dbo].[TD_VC_010] NULL,
[MM_DEST] [dbo].[TD_VC_002] NULL,
[NU_COMP_DEST] [dbo].[TD_VC_008] NULL,
[FE_OPER_DEST] [dbo].[TD_DT_001] NULL,
[FA_CAMB_DEST] [dbo].[TD_NU_015_006] NULL,
[IM_MOVI_DEST] [dbo].[TD_NU_016_004] NULL CONSTRAINT [DF__TCMOVI_CA__IM_MO__251C81ED] DEFAULT ((0)),
[CO_TIPO_DEST] [dbo].[TD_VC_003] NULL,
[CO_UNID_DEST] [dbo].[TD_VC_003] NULL,
[TI_ENTI_DEST] [dbo].[TD_VC_001] NULL,
[FE_REND_GAST] [dbo].[TD_DT_001] NULL,
[CO_ENTI_DEST] [dbo].[TD_VC_020] NULL,
[DE_OBSE_DEST] [dbo].[TD_VC_100] NULL,
[TI_DOCU_DEST] [dbo].[TD_VC_003] NULL,
[NU_DOCU_DEST] [dbo].[TD_VC_020] NULL,
[FA_CAMB_EXTR] [dbo].[TD_NU_015_006] NULL,
[CO_LIQU] [dbo].[TD_VC_020] NULL,
[NU_RECI_COBR] [dbo].[TD_VC_010] NULL,
[FE_RECI_COBR] [dbo].[TD_DT_001] NULL,
[TI_LIQU] [dbo].[TD_VC_003] NULL,
[NU_LIQU] [dbo].[TD_VC_020] NULL,
[ST_INTE] [dbo].[TD_VC_001_INTE] NULL CONSTRAINT [DF__TCMOVI_CA__ST_IN__2610A626] DEFAULT ('N'),
[FE_DIFE] [dbo].[TD_DT_001] NULL,
[CO_SUBR] [dbo].[TD_VC_003] NULL,
[TI_DOCU_ENRE] [dbo].[TD_VC_003] NULL,
[NU_DOCU_ENRE] [dbo].[TD_VC_020] NULL,
[IM_ENRE] [dbo].[TD_NU_016_004] NULL CONSTRAINT [DF__TCMOVI_CA__IM_EN__27F8EE98] DEFAULT ((0)),
[ST_CIER_ENRE] [dbo].[TD_VC_001] NULL CONSTRAINT [DF__TCMOVI_CA__ST_CI__28ED12D1] DEFAULT ('N'),
[NU_COBR] [dbo].[TD_VC_020] NULL,
[CO_TIEN] [dbo].[TD_VC_003] NULL,
[FE_GAST] [dbo].[TD_DT_001] NULL,
[CO_SUBR_DEST] [dbo].[TD_VC_003] NULL,
[CO_ORDE_SERV] [dbo].[TD_VC_020] NULL,
[TI_SITU] [dbo].[TD_VC_003] NULL,
[CO_USUA_CREA] [dbo].[TD_VC_008_USUA] NOT NULL CONSTRAINT [DF__TCMOVI_CA__CO_US__29E1370A] DEFAULT (suser_sname()),
[FE_USUA_CREA] [dbo].[TD_DT_001_SIST] NOT NULL CONSTRAINT [DF__TCMOVI_CA__FE_US__2AD55B43] DEFAULT (getdate()),
[CO_USUA_MODI] [dbo].[TD_VC_008_USUA] NOT NULL CONSTRAINT [DF__TCMOVI_CA__CO_US__2BC97F7C] DEFAULT (suser_sname()),
[FE_USUA_MODI] [dbo].[TD_DT_001_SIST] NOT NULL CONSTRAINT [DF__TCMOVI_CA__FE_US__2CBDA3B5] DEFAULT (getdate()),
[ST_APRO] [dbo].[TD_VC_001] NULL CONSTRAINT [DF_TCMOVI_CAJA_ST_APRO] DEFAULT ('N')
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO
CREATE      TRIGGER [dbo].[TD_TCMOVI_CAJA_D01] ON [dbo].[TCMOVI_CAJA] FOR DELETE
/*---------------------------------------------------------*/
/*---- Empresa              : OFISIS S.A.              ----*/
/*---- Cliente              : OFISIS S.A.              ----*/
/*---- Sistema              : OFITESO                  ----*/
/*---- M½dulo               : Empresa                  ----*/
/*---- Programa             : Trigger                  ----*/
/*---- Script               : TRIGGERS_TCMOVI_CAJA.SQL ----*/
/*---- Nombre TR            : TD_TCMOVI_CAJA_D01       ----*/
/*---- Desarrollado por     : Reynaldo Menacho Luyo  ----*/
/*---- Fecha Creaci½n       : 11/11/2004               ----*/
/*---- Base de Datos        : SQL_Server               ----*/
/*---- Versi½n              : 2000                      ----*/
/*---- Invoca a SP          :                          ----*/
/*---------------------------------------------------------*/
/*---- Modificado 1 por     :                          ----*/
/*---- Fecha Modificaci½n   :                          ----*/    
/*---- Detalle Modificacicn :                          ----*/  
/*---------------------------------------------------------*/
/*---- Comentarios: Genera Numero Correlativo, para    ----*/
/*----              la secuencia de ingreso de Compra  ----*/
/*----                                                 ----*/
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
/*---- Drop TRIGGER TD_TCMOVI_CAJA_D01                 ----*/
/*---- Grant All On TD_TCMOVI_CAJA_D01 to PUBLIC       ----*/
/*---------------------------------------------------------*/
NOT FOR REPLICATION  
As
Begin
  Declare
  @CSCO_EMPR       	TD_VC_002,
  @CSCO_CAJA       	TD_VC_008,
  @CSAA_CAJA  		TD_VC_004,
  @CSTI_MOVI  		TD_VC_001,
  @CSTI_SITU 		TD_VC_003,
  @CSCO_TIPO_OPER	TD_VC_003,
  @CSMM_CAJA  		TD_VC_002,
  @CNIM_MOVI  		TD_NU_016_004,
  @CSST_AUXI_0004	TD_VC_001,
  @VSDE_USER		TD_VC_008

  Declare CT_TCMOVI_CAJA_D01 Cursor
  For
  Select T1.CO_EMPR, T1.CO_CAJA, T1.AA_CAJA, T1.MM_CAJA, 
		 T1.TI_MOVI, T1.TI_SITU, T1.IM_MOVI, T2.ST_AUXI_0004
  From Deleted T1, TTTIPO_OPER T2
  Where T1.TI_SITU = 'ACT'
  And T1.CO_EMPR = T2.CO_EMPR
  And T1.CO_TIPO_OPER = T2.CO_TIPO_OPER
  And T2.ST_AUXI_0004 = 'N'

  Open  CT_TCMOVI_CAJA_D01
  Fetch CT_TCMOVI_CAJA_D01 Into
		@CSCO_EMPR, @CSCO_CAJA, @CSAA_CAJA, @CSMM_CAJA, 
		@CSTI_MOVI, @CSTI_SITU, @CNIM_MOVI, @CSST_AUXI_0004
  While @@Fetch_Status = 0
  Begin
		If @@Fetch_Status < 0
		Begin
			Close CT_TCMOVI_CAJA_D01
			Deallocate CT_TCMOVI_CAJA_D01
		End

		Select @VSDE_USER = System_User

		If @CSTI_MOVI = 'E' --And @CSTI_SITU = 'ACT'
		  Begin
			Update TMSACA_MENS			Set IM_EGRE = IM_EGRE - @CNIM_MOVI,
				CO_USUA_MODI = System_User, FE_USUA_MODI = Getdate()
			Where  CO_EMPR = @CSCO_EMPR
			And CO_CAJA = @CSCO_CAJA
			And AA_CAJA = @CSAA_CAJA
			And MM_CAJA = @CSMM_CAJA
		  End

		If @CSTI_MOVI = 'I' --And @CSTI_SITU = 'ACT'
		  Begin
			Update TMSACA_MENS			Set IM_INGR = IM_INGR - @CNIM_MOVI,
				CO_USUA_MODI = System_User, FE_USUA_MODI = Getdate()
			Where  CO_EMPR = @CSCO_EMPR
			And CO_CAJA = @CSCO_CAJA
			And AA_CAJA = @CSAA_CAJA
			And MM_CAJA = @CSMM_CAJA
		  End

		If Exists(Select CO_CAJA From   TMSACA_MENS
		    	Where CO_EMPR = @CSCO_EMPR And CO_CAJA = @CSCO_CAJA
		    	And AA_CAJA+MM_CAJA >= @CSAA_CAJA+@CSMM_CAJA)
		  Exec SP_TMSACA_MENS_U01 @CSCO_EMPR, @VSDE_USER, @CSCO_CAJA, @CSAA_CAJA, @CSMM_CAJA

      Fetch CT_TCMOVI_CAJA_D01 Into
        @CSCO_EMPR, @CSCO_CAJA, @CSAA_CAJA, @CSMM_CAJA, 
		@CSTI_MOVI, @CSTI_SITU, @CNIM_MOVI, @CSST_AUXI_0004
  End
  Close CT_TCMOVI_CAJA_D01
  Deallocate CT_TCMOVI_CAJA_D01
End
/*-------------------------------------- Fin de Trigger --------------------------------------*/

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE     TRIGGER [dbo].[TI_TCMOVI_CAJA_I01] ON [dbo].[TCMOVI_CAJA] FOR INSERT
/*---------------------------------------------------------*/
/*---- Empresa              : OFISIS S.A.              ----*/
/*---- Cliente              : OFISIS S.A.              ----*/
/*---- Sistema              : OFITESO                  ----*/
/*---- M½dulo               : Empresa                  ----*/
/*---- Programa             : Trigger                  ----*/
/*---- Script               : TRIGGERS_TCMOVI_CAJA.SQL ----*/
/*---- Nombre TR            : TI_TCMOVI_CAJA_I01       ----*/
/*---- Desarrollado por     : Reynaldo Menacho Luyo  ----*/
/*---- Fecha Creaci½n       : 11/11/2004               ----*/
/*---- Base de Datos        : SQL_Server               ----*/
/*---- Versi½n              : 2000                      ----*/
/*---- Invoca a SP          :                          ----*/
/*---------------------------------------------------------*/
/*---- Modificado 1 por     :                          ----*/
/*---- Fecha Modificaci½n   :                          ----*/    
/*---- Detalle Modificacicn :                          ----*/  
/*---------------------------------------------------------*/
/*---- Comentarios: Genera Numero Correlativo, para    ----*/
/*----              la secuencia de ingreso de Compra  ----*/
/*----                                                 ----*/
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
/*---- Drop TRIGGER tI_TCMOVI_CAJA_I01                 ----*/
/*---- Grant All On tI_TCMOVI_CAJA_I01 to PUBLIC       ----*/
/*---------------------------------------------------------*/
NOT FOR REPLICATION  
As
Begin
  Declare
  @CSCO_EMPR		TD_VC_002,
  @CSCO_CAJA    	TD_VC_020,
  @CSAA_CAJA  		TD_VC_004,
  @CSTI_MOVI  		TD_VC_001,
  @CSTI_SITU 		TD_VC_003,
  @CSMM_CAJA  		TD_VC_002,
  @CSCO_TIPO_OPER 	TD_VC_003,
  @CNIM_MOVI  		TD_NU_016_004,
  @VSDE_USER		TD_VC_008

  Declare CT_TCMOVI_CAJA_I01 Cursor
  For
  Select T1.CO_EMPR, T1.CO_CAJA, T1.AA_CAJA, T1.MM_CAJA, 
		 T1.TI_MOVI, T1.TI_SITU, T1.IM_MOVI
  From Inserted T1, TTTIPO_OPER T2
  Where T1.TI_SITU = 'ACT'
  And T1.CO_EMPR = T2.CO_EMPR
  And T1.CO_TIPO_OPER = T2.CO_TIPO_OPER
  And T2.ST_AUXI_0004 = 'N'
  
  Open  CT_TCMOVI_CAJA_I01
  Fetch CT_TCMOVI_CAJA_I01 Into
		@CSCO_EMPR, @CSCO_CAJA, @CSAA_CAJA, @CSMM_CAJA, 
		@CSTI_MOVI, @CSTI_SITU, @CNIM_MOVI
  While @@Fetch_Status = 0
  Begin
		If @@Fetch_Status < 0
		Begin
			Close CT_TCMOVI_CAJA_I01
			Deallocate CT_TCMOVI_CAJA_I01
		End

		Select @VSDE_USER = System_User

		If @CSTI_MOVI = 'E' --And @CSTI_SITU = 'ACT'
		  Begin
			Update TMSACA_MENS			Set IM_EGRE = IM_EGRE +  @CNIM_MOVI,
				CO_USUA_MODI = System_User, FE_USUA_MODI = Getdate()
			Where  CO_EMPR = @CSCO_EMPR
			And CO_CAJA = @CSCO_CAJA
			And AA_CAJA = @CSAA_CAJA
			And MM_CAJA = @CSMM_CAJA
		  End

		If @CSTI_MOVI = 'I' --And @CSTI_SITU = 'ACT'
		  Begin
			Update TMSACA_MENS			Set IM_INGR = IM_INGR +  @CNIM_MOVI,
				CO_USUA_MODI = System_User, FE_USUA_MODI = Getdate()
			Where  CO_EMPR = @CSCO_EMPR
			And CO_CAJA = @CSCO_CAJA
			And AA_CAJA = @CSAA_CAJA
			And MM_CAJA = @CSMM_CAJA
		  End

		If Exists(Select CO_CAJA From   TMSACA_MENS
		    	Where CO_EMPR = @CSCO_EMPR And CO_CAJA = @CSCO_CAJA
		    	And AA_CAJA+MM_CAJA >= @CSAA_CAJA+@CSMM_CAJA)
		  Exec SP_TMSACA_MENS_U01 @CSCO_EMPR, @VSDE_USER, @CSCO_CAJA, @CSAA_CAJA, @CSMM_CAJA

      Fetch CT_TCMOVI_CAJA_I01 Into
        @CSCO_EMPR, @CSCO_CAJA, @CSAA_CAJA, @CSMM_CAJA, 
		@CSTI_MOVI, @CSTI_SITU, @CNIM_MOVI
  End
  Close CT_TCMOVI_CAJA_I01
  Deallocate CT_TCMOVI_CAJA_I01
End
/*-------------------------------------- Fin de Trigger --------------------------------------*/

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

CREATE TRIGGER [dbo].[TU_TCMOVI_CAJA_U01] ON [dbo].[TCMOVI_CAJA] FOR UPDATE
/*---------------------------------------------------------*/
/*---- Empresa              : OFISIS S.A.              ----*/
/*---- Cliente              : OFISIS S.A.              ----*/
/*---- Sistema              : OFITESO                  ----*/
/*---- Módulo               : Empresa                  ----*/
/*---- Programa             : Trigger                  ----*/
/*---- Script               : TRIGGERS_TCMOVI_CAJA.SQL ----*/
/*---- Nombre TR            : TU_TCMOVI_CAJA_U01       ----*/
/*---- Desarrollado por     : Reynaldo Menacho Luyo  ----*/
/*---- Fecha Creación       : 11/11/2004               ----*/
/*---- Base de Datos        : SQL_Server               ----*/
/*---- Versión              : 2000                      ----*/
/*---- Invoca a SP          :                          ----*/
/*---------------------------------------------------------*/
/*---- Modificado 1 por     :                          ----*/
/*---- Fecha Modificación   :                          ----*/    
/*---- Detalle Modificacicn :                          ----*/  
/*---------------------------------------------------------*/
/*---- Comentarios: Genera Numero Correlativo, para    ----*/
/*----              la secuencia de ingreso de Compra  ----*/
/*----                                                 ----*/
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
/*---- Drop TRIGGER TU_TCMOVI_CAJA_U01                 ----*/
/*---- Grant All On TU_TCMOVI_CAJA_U01 to PUBLIC       ----*/
/*---------------------------------------------------------*/
NOT FOR REPLICATION  
As
If Update(IM_MOVI) Or Update(TI_SITU)
Begin
  Declare
  @CSCO_EMPR       	TD_VC_002,
  @CSCO_CAJA       	TD_VC_008,
  @CSAA_CAJA  		TD_VC_004,
  @CSTI_MOVI  		TD_VC_001,
  @CSTI_SITU 		TD_VC_003,
  @CSCO_TIPO_OPER	TD_VC_003,
  @CSMM_CAJA  		TD_VC_002,
  @CNIM_MOVI  		TD_NU_016_004,
  @CSST_AUXI_0004	TD_VC_001,
  @VSDE_USER		TD_VC_008,
  @CSNU_COMP_CAJA       TD_VC_008,
  @VSTI_SITU 		TD_VC_003

  Declare CT_TCMOVI_CAJA_U01 Cursor
  For
  Select T1.CO_EMPR, T1.CO_CAJA, T1.AA_CAJA, T1.MM_CAJA, T1.NU_COMP_CAJA, 
		 T1.TI_MOVI, T1.TI_SITU, T1.IM_MOVI, T2.ST_AUXI_0004
  From Deleted T1, TTTIPO_OPER T2
  Where T1.TI_SITU = 'ACT'
  And T1.CO_EMPR = T2.CO_EMPR
  And T1.CO_TIPO_OPER = T2.CO_TIPO_OPER
  And T2.ST_AUXI_0004 = 'N'

  Open  CT_TCMOVI_CAJA_U01
  Fetch CT_TCMOVI_CAJA_U01 Into
		@CSCO_EMPR, @CSCO_CAJA, @CSAA_CAJA, @CSMM_CAJA, @CSNU_COMP_CAJA, 
		@CSTI_MOVI, @CSTI_SITU, @CNIM_MOVI, @CSST_AUXI_0004
  While @@Fetch_Status = 0
  Begin
		If @@Fetch_Status < 0
		Begin
			Close CT_TCMOVI_CAJA_U01
			Deallocate CT_TCMOVI_CAJA_U01
		End


SELECT @VSTI_SITU = TI_SITU
FROM TCMOVI_CAJA
WHERE CO_EMPR = @CSCO_EMPR
AND CO_CAJA = @CSCO_CAJA
AND AA_CAJA = @CSAA_CAJA
AND MM_CAJA = @CSMM_CAJA
AND NU_COMP_CAJA = @CSNU_COMP_CAJA
AND TI_MOVI = @CSTI_MOVI

		If @CSTI_MOVI = 'E' And @VSTI_SITU != 'EXT'
		  Begin
			Update TMSACA_MENS			Set IM_EGRE = IM_EGRE - @CNIM_MOVI,
				CO_USUA_MODI = System_User, FE_USUA_MODI = Getdate()
			Where  CO_EMPR = @CSCO_EMPR
			And CO_CAJA = @CSCO_CAJA
			And AA_CAJA = @CSAA_CAJA
			And MM_CAJA = @CSMM_CAJA
		  End

		If @CSTI_MOVI = 'I' And @VSTI_SITU != 'EXT'
		  Begin
			Update TMSACA_MENS			Set IM_INGR = IM_INGR - @CNIM_MOVI,
				CO_USUA_MODI = System_User, FE_USUA_MODI = Getdate()
			Where  CO_EMPR = @CSCO_EMPR
			And CO_CAJA = @CSCO_CAJA
			And AA_CAJA = @CSAA_CAJA
			And MM_CAJA = @CSMM_CAJA
		  End
	

      Fetch CT_TCMOVI_CAJA_U01 Into
        @CSCO_EMPR, @CSCO_CAJA, @CSAA_CAJA, @CSMM_CAJA, @CSNU_COMP_CAJA, 
		@CSTI_MOVI, @CSTI_SITU, @CNIM_MOVI, @CSST_AUXI_0004
  End
  Close CT_TCMOVI_CAJA_U01
  Deallocate CT_TCMOVI_CAJA_U01

--------------------------------------------------------------

  Declare CT_TCMOVI_CAJA_U02 Cursor
  For
  Select T1.CO_EMPR, T1.CO_CAJA, T1.AA_CAJA, T1.MM_CAJA, T1.NU_COMP_CAJA,
		 T1.TI_MOVI, T1.TI_SITU, T1.IM_MOVI, T2.ST_AUXI_0004
  From Inserted T1, TTTIPO_OPER T2
  Where T1.TI_SITU = 'ACT'
  And T1.CO_EMPR = T2.CO_EMPR
  And T1.CO_TIPO_OPER = T2.CO_TIPO_OPER
  And T2.ST_AUXI_0004 = 'N'
  
  Open  CT_TCMOVI_CAJA_U02
  Fetch CT_TCMOVI_CAJA_U02 Into		@CSCO_EMPR, @CSCO_CAJA, @CSAA_CAJA, @CSMM_CAJA, @CSNU_COMP_CAJA,
		@CSTI_MOVI, @CSTI_SITU, @CNIM_MOVI, @CSST_AUXI_0004
  While @@Fetch_Status = 0
  Begin
		If @@Fetch_Status < 0
		Begin
			Close CT_TCMOVI_CAJA_U02
			Deallocate CT_TCMOVI_CAJA_U02
		End

		Select @VSDE_USER = System_User

SELECT @VSTI_SITU = TI_SITU
FROM TCMOVI_CAJA
WHERE CO_EMPR = @CSCO_EMPR
AND CO_CAJA = @CSCO_CAJA
AND AA_CAJA = @CSAA_CAJA
AND MM_CAJA = @CSMM_CAJA
AND NU_COMP_CAJA = @CSNU_COMP_CAJA
AND TI_MOVI = @CSTI_MOVI

		If @CSTI_MOVI = 'E' And @VSTI_SITU != 'EXT'
		  Begin
			Update TMSACA_MENS			Set    IM_EGRE = IM_EGRE + @CNIM_MOVI
			Where  CO_EMPR = @CSCO_EMPR
			And CO_CAJA = @CSCO_CAJA
			And AA_CAJA = @CSAA_CAJA
			And MM_CAJA = @CSMM_CAJA
		  End

		If @CSTI_MOVI = 'I' And @VSTI_SITU != 'EXT'
		  Begin
			Update TMSACA_MENS			Set    IM_INGR = IM_INGR + @CNIM_MOVI
			Where  CO_EMPR = @CSCO_EMPR
			And CO_CAJA = @CSCO_CAJA
			And AA_CAJA = @CSAA_CAJA
			And MM_CAJA = @CSMM_CAJA
		  End

		If Exists(Select CO_CAJA From   TMSACA_MENS
		    	Where CO_EMPR = @CSCO_EMPR And CO_CAJA = @CSCO_CAJA
		    	And AA_CAJA+MM_CAJA >= @CSAA_CAJA+@CSMM_CAJA)
		  Exec SP_TMSACA_MENS_U01 @CSCO_EMPR, @VSDE_USER, @CSCO_CAJA, @CSAA_CAJA, @CSMM_CAJA

      Fetch CT_TCMOVI_CAJA_U02 Into
        @CSCO_EMPR, @CSCO_CAJA, @CSAA_CAJA, @CSMM_CAJA, @CSNU_COMP_CAJA,
		@CSTI_MOVI, @CSTI_SITU, @CNIM_MOVI, @CSST_AUXI_0004
  End
  Close CT_TCMOVI_CAJA_U02
  Deallocate CT_TCMOVI_CAJA_U02

End
/*-------------------------------------- Fin de Trigger --------------------------------------*/


GO
ALTER TABLE [dbo].[TCMOVI_CAJA] WITH NOCHECK ADD CONSTRAINT [CK__TCMOVI_CA__ST_CO__2334397B] CHECK NOT FOR REPLICATION (([ST_CONT]='P' OR [ST_CONT]='N'))
GO
ALTER TABLE [dbo].[TCMOVI_CAJA] WITH NOCHECK ADD CONSTRAINT [CK__TCMOVI_CA__ST_IN__2704CA5F] CHECK NOT FOR REPLICATION (([ST_INTE]='P' OR [ST_INTE]='N'))
GO
ALTER TABLE [dbo].[TCMOVI_CAJA] ADD CONSTRAINT [PK__TCMOVI_C__69AE04062BDF596C] PRIMARY KEY CLUSTERED  ([CO_EMPR], [CO_CAJA], [AA_CAJA], [MM_CAJA], [TI_MOVI], [NU_COMP_CAJA]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[TCMOVI_CAJA] ADD CONSTRAINT [FK__TCMOVI_CAJA__0FED2634] FOREIGN KEY ([CO_EMPR], [CO_CAJA], [AA_CAJA], [MM_CAJA]) REFERENCES [dbo].[TMSACA_MENS] ([CO_EMPR], [CO_CAJA], [AA_CAJA], [MM_CAJA])
GO
ALTER TABLE [dbo].[TCMOVI_CAJA] ADD CONSTRAINT [FK__TCMOVI_CAJA__10E14A6D] FOREIGN KEY ([CO_EMPR], [CO_TIPO_OPER]) REFERENCES [dbo].[TTTIPO_OPER] ([CO_EMPR], [CO_TIPO_OPER])
GO
